package com.example.webexceldemo.utils;

import com.example.webexceldemo.pojo.Context;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;

public class WebJSoupUtils {
    private ArrayList<Context> contextArrayList;
    private static String url; // 网址
    private int page; // 页码
    public WebJSoupUtils(String url, int page) {
        this.url = url;
        this.page = page;
    }
    private void webJSoupUtils() throws IOException, InterruptedException {
        contextArrayList = new ArrayList<>();
        for (int i = 1; i < page + 1; i++) {
            String pageUrl =  url +  "/page/" + i;
            Document document = Jsoup.parse(new URL(pageUrl), 50000);
            Elements blogList = document.getElementsByClass("_1ySUUwWwmubujD8B44ZDzy");
            Elements span = blogList.select("span");
            System.out.println(pageUrl);
            for (Element element : span) {
                Context context = new Context();
                // 页码
                context.setPage(i);
                // 封面
                String path = element.getElementsByTag("img").eq(0).attr("src");
                context.setPath(path);
                // 标题
                String title = element.getElementsByClass("_3_JaaUmGUCjKZIdiLhqtfr").eq(0).text();
                context.setTitle(title);
                // 阅读数量
                String views = element.getElementsByClass("_2gvAnxa4Xc7IT14d5w8MI1").eq(0).text();
                context.setViews(Integer.valueOf(views));
                // 发表日期
                String date = element.getElementsByClass("_3TzAhzBA-XQQruZs-bwWjE").eq(0).text();
                context.setDate(date);
                contextArrayList.add(context);
            }
            Thread.sleep(2000);
        }
    }
    public ArrayList<Context> getContextArrayList() throws IOException, ParseException, InterruptedException {
        webJSoupUtils();
        return contextArrayList;
    }
}
